<?php

namespace App\Repositories\Article;

use App\Models\ArticleTag;
use App\Repositories\BaseRepository;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Cache;

/**
 * Class ArticleTagRepository
 * @package App\Repositories\Article
 * @method ArticleTag|null getById($id)
 * @method ArticleTag create(array $attributes)
 */
class ArticleTagRepository extends BaseRepository
{
    public function __construct(ArticleTag $model)
    {
        parent::__construct($model);
    }

    public function paginate(): LengthAwarePaginator
    {
        return ArticleTag::query()->orderByDesc('id')->paginate(10)->through(fn(ArticleTag $item) => [
            'id' => $item->id,
            'slug' => $item->slug,
            'name' => $item->name,
            'color' => $item->color,
            'created_at' => $item->created_at->format('Y-m-d'),
        ]);
    }

    public function getSelect(): array
    {
        return Cache::remember('article_tags_select',now()->addHour(),function (){
            return ArticleTag::query()->orderByDesc('id')->get(['id', 'name','slug', 'color'])->toArray();
        });
    }
}
